<?php 
namespace app\dboard\logic;
use think\Db;
use think\facade\Session;
class User extends Base
{

    function __construct()
    {
    }

    public function create($param)
    {
        $sid = isset($param['sid']) ? intval($param['sid']):0;
        $name = isset($param['name']) ? trim($param['name']):'';
        if (!$sid || !$name) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
            $this->res_data['Msg'] = "无此门店";
            return $this->res_data;
        }
        $data['sid'] =$sid;
        $data['name'] =$name;
        $data['status'] =1;
        $data['create_time'] =time();
        $data['update_time'] =time();
        $cdata = Db::name('user')->insertGetId($data);
        if(!$cdata) {
            $this->res_data['Msg'] = '添加班级失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }
    public function edit($param)
    {
        $cid = isset($param['cid']) ? intval($param['cid']):0;
        $sid = isset($param['sid']) ? intval($param['sid']):0;
        $name = isset($param['name']) ? trim($param['name']):'';
        $course_id1 = isset($param['course_id1']) ? trim($param['course_id1']):0;
        $course_id2 = isset($param['course_id2']) ? trim($param['course_id2']):0;
        $status = isset($param['status']) ? trim($param['status']):1;
        if (!$sid || !$name) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
            $this->res_data['Msg'] = "无此门店";
            return $this->res_data;
        }
        $user= Db::name('user')->where([['id','=',$cid]])->find();
        if (!$user) {
            $this->res_data['Msg'] = "无此班级";
            return $this->res_data;
        }
        $data['name'] =$name;
        $data['status'] =$status;
        $data['update_time'] =time();
        $course_id1 = trim($course_id1,',');//已开始
        $course_id2 = trim($course_id2,',');//未开始
        $data['course_id'] = $course_id1;
        if ($course_id2) {
            $data['course_id'] .= ','.$course_id2;
        }
        $course_id1_arr = explode(',', $course_id1);
        $course_id2_arr = explode(',', $course_id2);
        if (array_intersect($course_id1_arr,$course_id2_arr)) {
            $this->res_data['Msg'] = "已开始的课和未开始的课存在冲突";
            return $this->res_data;
        }
        if ($user['study_status']!=3) {
            $data['study_status'] = 3;
        }
        Db::startTrans();
        try {
            $cdata = Db::name('user')->where([['id','=',$cid]])->data($data)->update();
            if(!$cdata) {
                Db::rollback();
                $this->res_data['Msg'] = '编辑失败';
                return $this->res_data;
            }
            $arr = [];
            $user_course = Db::name('user_course')->where([['user_id','=',$cid],['status','in',[1,2]]])->select();
            if (count($user_course)) {
                foreach ($user_course as $k => $v) {
                    $arr[$v['course_id']] = $v['classtime_surplus'];
                }
                $is_update = Db::name('user_course')->where([['user_id','=',$cid],['status','in',[1,2]]])->update(['status'=>0,'updatetime'=>time()]);
                if(!$is_update) {
                    Db::rollback();
                    $this->res_data['Msg'] = '编辑失败';
                    return $this->res_data;
                }
            }
            $user_course_data = [];
            if ($course_id1) {//已开始
                $course_id_arr = explode(',', $course_id1);
                $course_id_arr = array_unique($course_id_arr);
                foreach ($course_id_arr as $key => $value) {
                    $course = Db::name('course')->field('id,periods')->where(['id'=>$value])->find();
                    $user_course_data[$key]['user_id'] = $cid;
                    $user_course_data[$key]['course_id'] = $value;
                    $user_course_data[$key]['status'] = 2;
                    $user_course_data[$key]['createtime'] = time();
                    $user_course_data[$key]['classtime_sum'] = $course['periods'];
                    if (isset($arr[$value])) {
                        $user_course_data[$key]['classtime_surplus'] = $arr[$value];
                    } else {
                        $user_course_data[$key]['classtime_surplus'] = $course['periods'];
                    }
                }
                $is_insert = Db::name('user_course')->insertAll($user_course_data);
                if(!$is_insert) {
                    Db::rollback();
                    $this->res_data['Msg'] = '编辑失败';
                    return $this->res_data;
                }
            }
            if ($course_id2) {//未开始
                $course_id_arr = [];
                $user_course_data = [];
                $course_id_arr = explode(',', $course_id2);
                $course_id_arr = array_unique($course_id_arr);
                foreach ($course_id_arr as $key => $value) {
                    $course = Db::name('course')->field('id,periods')->where(['id'=>$value])->find();
                    $user_course_data[$key]['user_id'] = $cid;
                    $user_course_data[$key]['course_id'] = $value;
                    $user_course_data[$key]['status'] = 1;
                    $user_course_data[$key]['createtime'] = time();
                    $user_course_data[$key]['classtime_sum'] = $course['periods'];
                    if (isset($arr[$value])) {
                        $user_course_data[$key]['classtime_surplus'] = $arr[$value];
                    } else {
                        $user_course_data[$key]['classtime_surplus'] = $course['periods'];
                    }
                }
                $is_insert = Db::name('user_course')->insertAll($user_course_data);
                if(!$is_insert) {
                    Db::rollback();
                    $this->res_data['Msg'] = '编辑失败';
                    return $this->res_data;
                }
            }
            Db::commit();
            $this->res_data['Msg'] = '成功';
            $this->res_data['Code'] = 0;
            return $this->printResult($this->res_data);
        } catch (\Exception $e) {
            Db::rollback();
            $this->res_data['Msg'] = '操作失败';
            return $this->res_data;
        }
    }
    public function delete($param)
    {
        $cid = isset($param['cid']) ? intval($param['cid']):0;
        $status = isset($param['status']) ? intval($param['status']):0;
        $user = Db::name('user')->where([['id','=',$cid]])->find();
        if (!$user) {
            $this->res_data['Msg'] = "无此班级";
            return $this->res_data;
        }
        $data['status'] =$status;
        $data['update_time'] =time();
        $cdata = Db::name('user')->where([['id','=',$cid]])->data($data)->update();
        if(!$cdata) {
            $this->res_data['Msg'] = '更新失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }
}

?>
